import { createRouter, createWebHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";
import Index from "../views/login/index.vue";
import { reactive } from "vue";
import { useCounterStore } from "@/stores/counter";
import axios from "axios";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "login",
      component: () => import("../views/login/index.vue"),
    },
    {
      path: "/home" ,
      name: "home",
      component: HomeView,
    },
    {
      path: "/about",
      name: "about",
      component: () => import("../views/AboutView.vue"),
    },
    {
      path: "/Main",
      name: "Main",
      component: () => import("../views/Main.vue"),
      children: [
        {
          path: "/WorkShop",
          name: "WorkShop",
          component: () => import("../views/Factory Modeling/WorkShop.vue"),
        },
        {
          path: "/Station",
          name: "Station",
          component: () => import("../views/Factory Modeling/Station.vue"),
        },
        {
          path: "/ProductionLine",
          name: "ProductionLine",
          component: () =>
            import("../views/Factory Modeling/ProductionLine.vue"),
        },
        {
          path: "/Bom",
          name: "Bom",
          component: () => import("../views/Material Management/Bom.vue"),
        },
        {
          path: "/MaterialGroup",
          name: "MaterialGroup",
          component: () =>
            import("../views/Material Management/MaterialGroup.vue"),
        },
        {
          path: "/MaterialAsk",
          name: "MaterialAsk",
          component: () =>
            import("../views/Material Management/MaterialAsk.vue"),
        },
        {
          path: "/ProductMaterials",
          name: "ProductMaterials",
          component: () =>
            import("../views/Material Management/ProductMaterials.vue"),
        },
        {
          path: "/PurchaseRequest",
          name: "PurchaseRequest",
          component: () =>
            import("../views/Material Management/PurchaseRequest.vue"),
        },
        //#region  用户管理
        {
          path: "/ClientSet",
          name: "ClientSet",
          component: () => import("../views/Account/ClientSet.vue"),
        },
        {
          path: "/SupplierSet",
          name: "SupplierSet",
          component: () => import("../views/Account/SupplierSet.vue"),
        },
        {
          path: "/RolePression",
          name: "RolePression",
          component: () => import("../views/Account/RolePression.vue"),
        },
        {
          path: "/DepartMentView",
          name: "DepartMentView",
          component: () => import("../views/Account/DepartMentView.vue"),
        },
        {
          path: "/DocumentCodeDialog",
          name: "DocumentCodeDialog",
          component: () => import("../views/Account/DocumentCodeDialog.vue"),
        },
        
        
        {
          path: "/Demo",
          name: "Demo",
          component: () => import("../views/Account/Demo.vue"),
        },
        //#endregion
        //#region 工艺工序
        {
          path: "/processCategorys",
          name: "processCategorys",
          component: () =>
            import("../views/processProcedures/processCategory/ShowView.vue"),
        },
        {
          path: "/ProcessManagement",
          name: "ProcessManagement",
          component: () =>
            import("../views/processProcedures/ProcessManagement/processmangView.vue"),
        },
        {
          path: "/ProcessFlow",
          name: "ProcessFlow",
          component: () =>
            import("../views/processProcedures/processFlow/ShowView.vue"),
        },
        {
          path: "/addProcessFlow",
          name: "addProcessFlow",
          component: () =>
            import("../views/processProcedures/processFlow/AddView.vue"),
        },
        //#endregion
        //#region 仓库
        {
          path: "/WarehouseShow",
          name: "WarehouseShow",
          component: () =>
            import("../views/Production Modules/WarehouseShow.vue"),
        },
        {
          path: "/InventoryShow",
          name: "InventoryShow",
          component: () =>
            import("../views/Production Modules/InventoryShow.vue"),
        },
         {
          path: "/GroupInventoryShow",
          name: "GroupInventoryShow",
          component: () =>
            import("../views/Production Modules/GroupInventoryShow.vue"),
        },
        {
          path: "/OutIn",
          name: "OutIn",
          component: () =>
            import("../views/Production Modules/OutIn.vue"),
        },
        {
          path: "/GroupOutIn",
          name: "GroupOutIn",
          component: () =>
            import("../views/Production Modules/GroupOutIn.vue"),
        },
        {
          path: "/Producetion",
          name: "Producetion",
          component: () =>
            import("../views/Production Modules/ProducetionView.vue"),
        },
        {
          path: "/MaterialOutAsk",
          name: "MaterialOutAsk",
          component: () =>
            import("../views/Production Modules/MaterialOutAsk.vue"),
        },
        //#endregion
        //#region 工单
        {
          path: "/Scheduling",
          name: "Scheduling",
          component: () =>
            import("../views/Order Module/Scheduling.vue"),
        },
         //#endregion

           //#region 质检ls
        // {
        //   path: "/QualityInspection",
        //   name: "QualityInspection",
        //   component: () =>
        //     import("../views/QualityInspection/QualityInspection.vue"),
        // },
        {
          path: "/QualityInspections",
          name: "QualityInspections",
          component: () =>
            import("../views/QualityInspection/QualityInspections.vue"),
        },
        {
          path: "/QualityInspectionAnalysis",
          name: "QualityInspectionAnalysis",
          component: () =>
            import("../views/QualityInspection/QualityInspectionAnalysis.vue"),
        },
        {
          path: "/UnqualifiedProductHandler",
          name: "UnqualifiedProductHandler",
          component: () =>
            import("../views/QualityInspection/UnqualifiedProductHandler.vue"),
        },
        {
          path: "/QualityStandardManager",
          name: "QualityStandardManager",
          component: () =>
            import("../views/QualityInspection/QualityStandardManager.vue"),
        },
        

        
         //#endregion
        //#region 仪表盘
        {
          path: "/Dashboard",
          name: "Dashboard",
          component: () =>
            import("../views/dashboard/dashboardShow.vue"),
        },{
          path: "/Weather",
          name: "Weather",
          component: () =>
            import("../views/dashboard/Weather.vue"),
        },
        //#endregion
         //#region 出票到票
        {
          path: "/AddInvoice",
          name: "AddInvoice",
          component: () =>
            import("../views/InvoiceTicket/AddInvoice.vue"),
        },{
          path: "/AddTicket",
          name: "AddTicket",
          component: () =>
            import("../views/InvoiceTicket/AddTicket.vue"),
        },
        {
          path: "/Order",
          name: "Order",
          component: () =>
            import("../views/Order Module/OrderShowView.vue"),
        },
        {
          path: "/addcs",
          name: "addcs",
          component: () =>
            import("../views/InvoiceTicket/addcs.vue"),
        },
        {
          path: "/addin",
          name: "addin",
          component: () =>
            import("../views/InvoiceTicket/addin.vue"),
        },
        {
          path: "/upd",
          name: "upd",
          component: () =>
            import("../views/InvoiceTicket/updmomeny.vue"),
        },
        {
          path: "/getInvoicing",
          name: "getInvoicing",
          component: () =>
            import("../views/InvoiceTicket/getInvoicing.vue"),
        },
        {
          path: "/getTicket",
          name: "getTicket",
          component: () =>
            import("../views/InvoiceTicket/getTicket.vue"),
        },
        //#endregion

      ],
    },
    //登录
    
  ],
});
//导航守卫
// router.beforeEach((to, from, next) => {
//   const pinia = useCounterStore();
  
//   // 检查localStorage中的token
//   const token = window.localStorage.getItem('token');
  
//   // 如果localStorage中有token，确保pinia中的登录状态也是正确的
//   if (token && !pinia.piniaDto.IsLoign) {
//     // 如果localStorage中有token但pinia中显示未登录，则更新pinia状态
//     // 这种情况通常发生在页面刷新后
//     pinia.piniaDto.IsLoign = true;
    
//     // 设置axios请求头
//     axios.defaults.headers.common['Authorization'] = `Bearer ${token}`;
//   }
  
//   // 如果用户未登录（检查更新后的pinia状态）
//   if (!pinia.piniaDto.IsLoign) {
//     // 如果访问的不是登录页，则重定向到登录页
//     if (to.path !== "/") {
//       next("/");
//     } else {
//       // 如果访问的就是登录页，则允许访问
//       next();
//     }
//   } 
//   // 如果用户已登录
//   else {
//     // 如果用户访问登录页，则重定向到工作台
//     if (to.path === "/") {
//       next("/Dashboard");
//     } else {
//       // 其他情况正常导航
//       next();
//     }
//   }
// });
//导航守卫       登录路径为  "/"
router.beforeEach((to, from, next) => {
  const pinia = useCounterStore();
  if (to.path != "/" && pinia.piniaDto.IsLoign == false) {
    next("/");
  } else {
    next();
  }
});
export default router;
